/*
 * DataReader.cpp
 *
 *  Created on: Oct 1, 2010
 *      Author: blyth
 */

#include "DataReader.h"
#include "dataForm.h"
#include "TH2D.h"
#include "TFile.h"
#include "TBranch.h"
#include <sstream>
#include <cstring>
#include <iostream>
#include "globals.hh"
using namespace std;

DataReader::DataReader(char* path) {
	file = new TFile(path);
	index = 0;
}

DataReader::~DataReader() {
	file->Close();
}

bool DataReader::ReadNext(TH2D* hist)
{
	stringstream ss;
	ss << "kaonNumber" << index;
	TTree* tree = (TTree*)file->Get((ss.str()).c_str());
	if(!tree)
	{
		cout << "End of Trees\n";
		return false;
	}

	dataForm** data = new dataForm*[3];
	for(int i = 0;i < 3;i++) data[i] = new dataForm;

	for(int i = 0;i < 3;i++)
	{
		stringstream ss1;
		ss1 << "pionNumber" << i;
		TBranch* branch = tree->GetBranch((ss1.str()).c_str());
		branch->SetAddress(data[i]);

		int count = branch->GetEntryNumber();
		for(int j = 0;j < count;j++)
		{
			branch->GetEntry(j);
			hist->Fill((data[i]->x)/m,(data[i]->y)/m,data[i]->energy);
		}
	}

	for(int i = 0;i < 3;i++) delete data[i];
	delete data;
	delete tree;

	index++;
	return true;
}
